// component/filterCard/filterCard.js
Component({
  options: {
    styleIsolation: 'shared'
  },
  /**
   * 组件的属性列表
   */
  properties: {
    typeList:{
      type:Array
    },
    filterType:{
      type:Array
    },
    filterState:{
      type:Array
    },
    workTypeList:{
      type:Array
    },
    cardType:{

    },
    filterActive:{
      type:Object
    },
    bottomBool:{
      type:Boolean,
      value:true,
    }
  },
  observers:{
    'typeList':function (val){
      this.setData({
        listData:val,
      })
    },
    'filterActive':function (val){
      if(val){
        let index=this.data.listData.findIndex((el)=>{
          return  el.id==val.id
        })
        console.log(index,'009index------')
        if(index===0){
          this.setData({
            mainActiveIndex:index,
            activeId:0,
            menuTypeValue:val.text
          })
        }else {
          this.setData({
            mainActiveIndex:index,
            activeId:'',
            menuTypeValue:val.text
          })
        }

      }

    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    listData:null,
    filterShow: false,
    menuTypeValue: "全部分类",
    mainActiveIndex: 1, //左侧选中项的索引
    activeId: 0, //右侧选中项的id
    dataInfo:{
      pattern:0,
      workType:0,
      state:0,
    },
    startCash:'',
    endCash:'',
    contributionsSort:false,//投稿数箭头
    receivedSort:false, //威客好评数
    talentReceivedSort:false, //服务商好评数
    sortTimeChange:false,  //最新
    salesSort:false, //销量,
    talenTime:false,
    talenTreceived:false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 服务商最新
     * @param
     */
    talentTime(){
      const {talenTime}=this.data

      let index
      if(talenTime){
        index=''
        this.setData({
          talenTime:!this.data.talenTime
        })
      }else {
        index=2
        this.setData({
          talenTime:!this.data.talenTime,
          talenTreceived:false
        })
      }
      let data={
        index,
      }
      this.triggerEvent('talentTime', data)
    },

    /**
     * 服务商好评数
     */
    talenTreceived(){
      const {talenTreceived}=this.data
      let index
      if(talenTreceived){
        index=''
        this.setData({
          talenTreceived:!this.data.talenTreceived
        })
      }else {
        index=1
        this.setData({
          talenTreceived:!this.data.talenTreceived,
          talenTime:false
        })
      }
      let data={
        index,
      }
      this.triggerEvent('talenTreceived', data)

    },


    /**
     * 好评数
     */
    received(){
      let data={
        index: ''
      }
      this.setData({
        receivedSort: !this.data.receivedSort,
      });
      if(this.data.receivedSort){
        data={
          index: 3
        }
      }
      this.triggerEvent('received', data)
    },
    /**
     * 投稿数
     */
    contributions(){
        let data={
          index: ''
        }
        this.setData({
          contributionsSort: !this.data.contributionsSort,
        });
      if(this.data.contributionsSort){
        data={
          index: 3
        }
      }
        this.triggerEvent('contributions', data)
    },
    /**
     * 最新
     */
    sortTime(){
      let data={
        index: ''
      }
      this.setData({
        sortTimeChange: !this.data.sortTimeChange,
      });
      if(this.data.sortTimeChange){
        data={
          index: 1
        }
      }
      this.triggerEvent('sortTime', data)
    },

    sales(){
      let data={
        index: ''
      }
      this.setData({
        salesSort: !this.data.salesSort,
      });
      if(this.data.salesSort){
        data={
          index: 2
        }
      }
      this.triggerEvent('sales', data)
    },
    /**
     * 右侧分类
     * @param detail
     */
    onClickNav({
                 detail = {}
               }) {
      this.setData({
        mainActiveIndex: detail.index || 0,
      });
    },

    /**
     * 分类筛选
     * @param detail
     */
    onClickItem({
                  detail = {}
                }) {
      const activeId = detail.id ;
      this.setData({
        activeId,
        menuTypeValue: detail.text
      });
      this.onConfirm()
      this.triggerEvent('filterSelect', detail)

    },

    filterChange(){
      let {startCash,endCash}=this.data
      let filterData=this.data.dataInfo
      filterData.startCash=startCash
      filterData.endCash=endCash
      this.triggerEvent('filterChange', filterData)
      this.onClose()
    },

    filterClear(){
      this.clearData()
      this.filterChange()
    },

    //清空
    clearData(){
      let data={
        pattern:0,
        workType:0,
        state:0
      }
      this.setData({
        dataInfo:data,
        startCash:'',
        endCash:''
      });
    },



    onConfirm() {
      this.selectComponent('#filterCard').toggle();
    },



    selectChange(value){
      let e = value.currentTarget.dataset
      let obj_str='dataInfo.'+e.name
      this.setData({
        [obj_str]:e.value
      })
    },

    filterShowChange(e) {
      this.setData({
        filterShow: true
      })
    },

    onClose() {
      this.setData({
        filterShow: false
      })
    },
  }
})
